/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.msg.hrappy.backend.repository.beans;

import edu.msg.hrappy.backend.model.DepartmentType;
import edu.msg.hrappy.backend.repository.DepartmentTypeRepository;
import edu.msg.hrappy.backend.repository.exception.RepositoryException;
import javax.ejb.Stateless;
import javax.persistence.TypedQuery;

/**
 *
 * @author toroke
 */
@Stateless
public class DepartmentTypeRepositoryBean  extends BaseRepositoryBean<DepartmentType, Long> implements DepartmentTypeRepository{

    public DepartmentTypeRepositoryBean() {
        super(DepartmentType.class);
    }

    @Override
    public DepartmentType getByName(String name) {
        try {
            TypedQuery<DepartmentType> query = (TypedQuery<DepartmentType>) getManager ().createQuery (
                    "SELECT t FROM DepartmentType t WHERE t.type = :name");
            query.setParameter ("name", name);
            
            if (query.getResultList ().isEmpty ()) {
                return null;
            } else {
                return query.getSingleResult ();
            }
        } catch (final javax.ejb.TransactionRolledbackLocalException ex) {
            throw new RepositoryException ("Get by name failed", ex);
        }
    }
    
}
